package com.kuaimacode.kframework.code.template;

import com.kuaimacode.kframework.code.domain.Generate;
import com.kuaimacode.kframework.code.enums.FieldInputType;
import com.kuaimacode.kframework.code.utils.GenerateUtil;
import com.kuaimacode.kframework.code.utils.TemplateUtil;
import com.kuaimacode.kframework.util.ToolUtil;

import java.nio.file.FileAlreadyExistsException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 * @date 2018/10/25
 */
public class AddHtmlTemplate {

    /**
     * 生成页面
     */
    private static String genHtmlBody(Generate generate) {
        // 构建数据
        String var = ToolUtil.lowerFirst(generate.getBasic().getTableEntity());
        String title = generate.getBasic().getGenTitle();
        String filePath = AddHtmlTemplate.class.getResource("").getPath()
                + AddHtmlTemplate.class.getSimpleName() + ".code";

        // 提取html页面
        String htmlTarget = TemplateUtil.getTemplate(filePath, "html");

        // 遍历字段
        String fieldTarget = TemplateUtil.getTemplate(filePath, "field");
        String selectTarget = TemplateUtil.getTemplate(filePath, "select");
        String textareaTarget = TemplateUtil.getTemplate(filePath, "textarea");
        StringBuilder fieldBuilder = new StringBuilder();
        String[] ignore = {"id", "remark", "createAt", "updateAt", "createBy", "updateBy", "status"};
        List<String> ignoreList = Arrays.asList(ignore);
        final boolean[] fieldRemark = {false};
        generate.getFields().forEach(field -> {
            if(!ignoreList.contains(field.getName())){
                if(field.getDict() != null && !"".equals(field.getDict())){
                    String temp = selectTarget;
                    temp = temp.replace("#{field.title}", field.getTitle());
                    temp = temp.replace("#{field.dict}", field.getDict());
                    temp = temp.replace("#{field.name}", field.getName());
                    fieldBuilder.append(temp);
                }else{
                    if(FieldInputType.Textarea.getCode().equals(field.getInputType())){
                        String temp = textareaTarget;
                        temp = temp.replace("#{field.title}", field.getTitle());
                        temp = temp.replace("#{field.name}", field.getName());
                        fieldBuilder.append(temp);
                    }else{
                        String temp = fieldTarget;
                        temp = temp.replace("#{field.title}", field.getTitle());
                        temp = temp.replace("#{field.name}", field.getName());
                        fieldBuilder.append(temp);
                    }
                }
            }
            if(field.getName().equals("remark")){
                fieldRemark[0] = true;
            }
        });
        htmlTarget = htmlTarget.replace(fieldTarget, fieldBuilder);
        htmlTarget = htmlTarget.replace(selectTarget, "");
        htmlTarget = htmlTarget.replace(textareaTarget, "");

        // 判断是否需要remark字段
        String remarkTarget = TemplateUtil.getTemplate(filePath, "remark");
        if (!fieldRemark[0]){
            htmlTarget = htmlTarget.replace(remarkTarget, "");
        }

        // 替换基本数据
        htmlTarget = htmlTarget.replace("#{var}", var);
        htmlTarget = htmlTarget.replace("#{title}", title);
        return htmlTarget;
    }

    /**
     * 生成列表页面模板
     */
    public static String generate(Generate generate) {
        String filePath = GenerateUtil.getHtmlFilePath(generate, "form");
        String content = AddHtmlTemplate.genHtmlBody(generate);
        try {
            GenerateUtil.generateFile(filePath, content);
        } catch (FileAlreadyExistsException e) {
            return GenerateUtil.fileExist(filePath);
        }
        return filePath;
    }
}
